<?php

/**
 * user actions.
 *
 * @package    jiazhang
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class userActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
      $old = (int)$request->getParameter('old',0);
      
      if($old > 0){
          $year = date('Y');
          $old_year = ($year - $old)+1;
          $user_ids = Doctrine::getTable('UserChild')->createQuery()->where('year = ?',$old_year)->execute()->toKeyValueArray('user_id', 'user_id');
          if(count($user_ids) == 0 ){
              $dql = Doctrine::getTable('User')->createQuery()->where('id = 0');
          }else{
              $dql = Doctrine::getTable('User')->createQuery()->whereIn('id',$user_ids)->orderBy('created_at desc');
          }
      }else{
          $dql = Doctrine::getTable('User')->createQuery()->orderBy('created_at desc');
      }
      
        $page = $request->getParameter('page',1);
        $this->pager = new sfDoctrinePager('User',30);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
  }
  
  public function executeShow(sfWebRequest $request)
  {
        $id = (int)$request->getParameter('id',0);
        $this->forward404Unless($this->user = Doctrine::getTable('User')->findOneById($id));
  }
  
  public function executeChange_user_status(sfWebRequest $request){
       $status = (int)$request->getParameter('status',1);
       $id = (int)$request->getParameter('id',0);
       $this->forward404Unless($user = Doctrine::getTable('User')->findOneById($id));
       $user->setStatus($status)->save();
       $this->getUser()->setFlash('success', '帐号更新成功！');
       $this->redirect('user/index');
  }
  
}
